{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e8deae88",
   "metadata": {},
   "source": [
    "# 文件读写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d2f60bb",
   "metadata": {},
   "source": [
    "## 修改路径：将路径转换到数据文件夹下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "62b208b9",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "current path:  c:\\Users\\illus\\OneDrive\\GIT\\data-analysis\\lecture-python\\doc\n",
      "new path :  C:\\Users\\illus\\OneDrive\\GIT\\data-analysis\\lecture-python\\raw-data\n",
      "['baby_trade_history.csv', 'bond_intraday_trade.csv', 'cbond-interest-info.xlsx', 'cfps2018_famconf_demo.csv', 'meal_order_detail.xlsx', 'sam_tianchi_mum_baby.csv', 'titanic.csv', 'titanic.xlsx', 'titanic2.csv']\n"
     ]
    }
   ],
   "source": [
    "# 导入os模块  \n",
    "import os\n",
    "\n",
    "# 查看当前路径\n",
    "print('current path: ', os.getcwd())   \n",
    "\n",
    "# 更改路径\n",
    "data_path = r'C:\\Users\\illus\\OneDrive\\GIT\\data-analysis\\lecture-python\\raw-data'  # 前面加r, 防止转义\n",
    "os.chdir(data_path)\n",
    "print('new path : ', os.getcwd()) \n",
    "print(os.listdir())  # 查看当前文件夹下文件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b835e8c2",
   "metadata": {},
   "source": [
    "## 读取文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4ffd2357",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b497ae3",
   "metadata": {},
   "source": [
    "###  csv格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "63e2d67f",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "df  = pd.read_csv('titanic.csv', dtype = {'PassengerId':str})   \n",
    "    # 结果为dataframe 格式； \n",
    "    # 可加选择\n",
    "        # encoding 默认为 utf-8; other 编码： gbk, gbk2312, gbk18030\n",
    "        # nrow = 100\n",
    "    \n",
    "#pd.read_csv?  # 查看方法说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "197cf3e0",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  PassengerId  Survived  Pclass  \\\n",
       "0           1         0       3   \n",
       "1           2         1       1   \n",
       "2           3         1       3   \n",
       "3           4         1       1   \n",
       "4           5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数据\n",
    "df.head() #默认为前5行\n",
    "# df.tail()  #默认为倒数5行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1b476f6f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 891 entries, 0 to 890\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  891 non-null    object \n",
      " 1   Survived     891 non-null    int64  \n",
      " 2   Pclass       891 non-null    int64  \n",
      " 3   Name         891 non-null    object \n",
      " 4   Sex          891 non-null    object \n",
      " 5   Age          714 non-null    float64\n",
      " 6   SibSp        891 non-null    int64  \n",
      " 7   Parch        891 non-null    int64  \n",
      " 8   Ticket       891 non-null    object \n",
      " 9   Fare         891 non-null    float64\n",
      " 10  Cabin        204 non-null    object \n",
      " 11  Embarked     889 non-null    object \n",
      "dtypes: float64(2), int64(4), object(6)\n",
      "memory usage: 83.7+ KB\n"
     ]
    }
   ],
   "source": [
    "# 获取数据基本信息\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "446c6f0d",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>714.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "      <td>891.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.383838</td>\n",
       "      <td>2.308642</td>\n",
       "      <td>29.699118</td>\n",
       "      <td>0.523008</td>\n",
       "      <td>0.381594</td>\n",
       "      <td>32.204208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.486592</td>\n",
       "      <td>0.836071</td>\n",
       "      <td>14.526497</td>\n",
       "      <td>1.102743</td>\n",
       "      <td>0.806057</td>\n",
       "      <td>49.693429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.420000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>20.125000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.910400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.454200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>31.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>80.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>512.329200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Survived      Pclass         Age       SibSp       Parch        Fare\n",
       "count  891.000000  891.000000  714.000000  891.000000  891.000000  891.000000\n",
       "mean     0.383838    2.308642   29.699118    0.523008    0.381594   32.204208\n",
       "std      0.486592    0.836071   14.526497    1.102743    0.806057   49.693429\n",
       "min      0.000000    1.000000    0.420000    0.000000    0.000000    0.000000\n",
       "25%      0.000000    2.000000   20.125000    0.000000    0.000000    7.910400\n",
       "50%      0.000000    3.000000   28.000000    0.000000    0.000000   14.454200\n",
       "75%      1.000000    3.000000   38.000000    1.000000    0.000000   31.000000\n",
       "max      1.000000    3.000000   80.000000    8.000000    6.000000  512.329200"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取数值数据的描述性分析\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cf89987",
   "metadata": {},
   "source": [
    "### Excel 格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "8b390ca9",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "df = pd.read_excel('titanic.xlsx',sheet_name = 'titanic', dtype = {'PassengerId':str})\n",
    "\n",
    "# pd.read_excel?\n",
    "# df.head()\n",
    "# df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64b03818",
   "metadata": {},
   "source": [
    "### dta格式 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "9a790bdc",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "file_path_famconf = r'C:\\\\Users\\\\illus\\\\OneDrive\\\\B-Database\\\\中国家庭追踪调查-data\\\\CFPS-data\\\\ecfps2018famconf_202008.dta'\n",
    "data_famconf = pd.read_stata(file_path_famconf, convert_categoricals=False) \n",
    "#如果报错：value labels for column wm703 are not unique.解决方法  convert_categoricals=False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3d956957",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(58504, 296)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_famconf.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "49e3db37",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>fid18</th>\n",
       "      <th>fid_provcd18</th>\n",
       "      <th>fid_countyid18</th>\n",
       "      <th>fid_cid18</th>\n",
       "      <th>fid_urban18</th>\n",
       "      <th>pid</th>\n",
       "      <th>fid_base</th>\n",
       "      <th>psu</th>\n",
       "      <th>fid10</th>\n",
       "      <th>fid12</th>\n",
       "      <th>...</th>\n",
       "      <th>ads1_18</th>\n",
       "      <th>kz103_18</th>\n",
       "      <th>interrupt18</th>\n",
       "      <th>sresppid18</th>\n",
       "      <th>kz1pid18</th>\n",
       "      <th>cyear18</th>\n",
       "      <th>cmonth18</th>\n",
       "      <th>iwmode18</th>\n",
       "      <th>interviewerid18</th>\n",
       "      <th>releaseversion</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100051.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>624942.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100051501.0</td>\n",
       "      <td>110043.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100051502.0</td>\n",
       "      <td>100051502.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>761040.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100051.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>624942.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>110043107.0</td>\n",
       "      <td>110043.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>110043.0</td>\n",
       "      <td>110043.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100051502.0</td>\n",
       "      <td>100051502.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>761040.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>100051.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>624942.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100051502.0</td>\n",
       "      <td>110043.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100051502.0</td>\n",
       "      <td>100051502.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>761040.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>100160.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>-9.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100160601.0</td>\n",
       "      <td>120009.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>-8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>120009102.0</td>\n",
       "      <td>120009102.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>459505.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>100160.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>-9.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>120009102.0</td>\n",
       "      <td>120009.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>120009.0</td>\n",
       "      <td>120009.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>120009102.0</td>\n",
       "      <td>120009102.0</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>459505.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 296 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      fid18  fid_provcd18  fid_countyid18  fid_cid18  fid_urban18  \\\n",
       "0  100051.0          11.0            45.0   624942.0          1.0   \n",
       "1  100051.0          11.0            45.0   624942.0          1.0   \n",
       "2  100051.0          11.0            45.0   624942.0          1.0   \n",
       "3  100160.0          12.0            79.0       -9.0          1.0   \n",
       "4  100160.0          12.0            79.0       -9.0          1.0   \n",
       "\n",
       "           pid  fid_base   psu     fid10     fid12  ...  ads1_18  kz103_18  \\\n",
       "0  100051501.0  110043.0  45.0      -8.0      -8.0  ...      0.0       1.0   \n",
       "1  110043107.0  110043.0  45.0  110043.0  110043.0  ...      0.0       1.0   \n",
       "2  100051502.0  110043.0  45.0      -8.0      -8.0  ...      0.0       1.0   \n",
       "3  100160601.0  120009.0  79.0      -8.0      -8.0  ...      0.0       1.0   \n",
       "4  120009102.0  120009.0  79.0  120009.0  120009.0  ...      0.0       1.0   \n",
       "\n",
       "   interrupt18   sresppid18     kz1pid18  cyear18  cmonth18  iwmode18  \\\n",
       "0          0.0  100051502.0  100051502.0   2018.0      10.0       2.0   \n",
       "1          0.0  100051502.0  100051502.0   2018.0      10.0       2.0   \n",
       "2          0.0  100051502.0  100051502.0   2018.0      10.0       2.0   \n",
       "3          0.0  120009102.0  120009102.0   2018.0       8.0       1.0   \n",
       "4          0.0  120009102.0  120009102.0   2018.0       8.0       1.0   \n",
       "\n",
       "   interviewerid18  releaseversion  \n",
       "0         761040.0             1.0  \n",
       "1         761040.0             1.0  \n",
       "2         761040.0             1.0  \n",
       "3         459505.0             1.0  \n",
       "4         459505.0             1.0  \n",
       "\n",
       "[5 rows x 296 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data_famconf.head()\n",
    "data_famconf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "703762b0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# pd.set_option('display.max_columns',20)\n",
    "# pd.set_option('display.max_rows',100)\n",
    "# 这里设定无效。。。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc5b7a71",
   "metadata": {},
   "source": [
    "## 保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de942acc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 保存为csv\n",
    "data_save_path = r'C:\\Users\\illus\\OneDrive\\A-Teaching\\Data-Analysis\\data'\n",
    "df.to_csv(os.path.join(data_save_path,'titanic_adjust.csv'),index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a3188856",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# 保存为excel 格式\n",
    "df.to_excel(os.path.join(data_save_path,'titanic_adjust.xlsx'),index=False, sheet_name = 'titanic')\n",
    "\n",
    "# DataFrame.to_excel(excel_writer, sheet_name='Sheet1', na_rep='',  float_format=None, columns=None, header=True, index=True, \n",
    "# index_label=None, startrow=0, startcol=0, engine=None,  merge_cells=True, encoding=None, inf_rep='inf', verbose=True, \n",
    "# freeze_panes=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41cf5d88",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存为excel格式的多个sheet\n",
    "\n",
    "# writer = pd.ExcelWriter(r\"file_name.xlsx\")\n",
    "# df1.to_excel(writer, sheet_name=\"sheet1\", index=False)\n",
    "# df2.to_excel(writer, sheet_name=\"sheet2\", index=False)\n",
    "# writer.save()\n",
    "# writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "240572ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上面的方法会将原来的excel文件覆盖掉，假如想要对已经存在的excel文件进行修改，可以使用开源工具包（anaconda已附带）openpyxl\n",
    "# import pandas as pd\n",
    "# from openpyxl import load_workbook\n",
    "# writer = pd.ExcelWriter('test.xlsx',engin='openpyxl')\n",
    "# book = load_workbook(writer.path)\n",
    "# writer.book = book\n",
    "# df.to_excel(excel_writer=writer,sheet_name=\"info\")\n",
    "# writer.save()\n",
    "# writer.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
